package com.yiyuan.app.config;

import com.alipay.api.*;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
@AllArgsConstructor
public class AliPayConfig {

    private final AliPayProperties properties;

    /**
     * 微信小程序支付
     */
    @Bean
    public AlipayClient alipayClient() throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setAppId(properties.getAppId());
        alipayConfig.setPrivateKey(properties.getPrivateKey());
        alipayConfig.setAppCertPath(properties.getAppCertPath());
        alipayConfig.setAlipayPublicCertPath(properties.getAliPayPublicCertPath());
        alipayConfig.setRootCertPath(properties.getRootCertPath());
        //设置连接池中的最大可缓存的空闲连接数
        alipayConfig.setMaxIdleConnections(properties.getMaxIdleConnections());
        //连接超时，单位：毫秒，默认3000
        alipayConfig.setConnectTimeout(properties.getConnectTimeout());
        //读取超时，单位：毫秒，默认15000
        alipayConfig.setReadTimeout(properties.getReadTimeout());
        //空闲连接存活时间，单位：毫秒，默认10000L
        alipayConfig.setKeepAliveDuration(properties.getKeepAliveDuration());
        alipayConfig.setEncryptKey(properties.getEncryptKey());
        return new DefaultAlipayClient(alipayConfig);
    }

}
